home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Techniques / Example by Cortis Clark / Make your code faster! / Mandelbrot in C / Mandelbrot.cpp < prev    next >
Text File  |  2000-04-07  |  2KB  |  117 lines

  1. // by G. Cortis Clark
  2.  
  3. #include <Palettes.h>
  4. #include "Mandelbrot.h"
  5.  
  6.  
  7. inline complex complexSquare (complex c) {
  8.   complex cSq;
  9.  
  10.   cSq.x = c.x * c.x - c.y * c.y;
  11.   cSq.y = 2 * c.x * c.y;
  12.   return (cSq);
  13. }
  14.  
  15. int iterate (complex zInit) {
  16.   complex z = zInit;
  17.   int cnt = 0;
  18.  
  19.   //  quit when z * z > 4
  20.   while ((z.x * z.x + z.y * z.y <= 4.0) && (cnt < MAX)) {
  21.       if (zInit.x * zInit.x + zInit.y * zInit.y <= 4.0) {
  22.         z = complexSquare(z);
  23.         z.x += zInit.x;
  24.         z.y += zInit.y;
  25.         cnt++;
  26.     } else {
  27.         z = complexSquare(z);
  28.         z.x += 4;
  29.         z.y += 4;
  30.         cnt++;
  31.     }
  32.     
  33.   }
  34.   return (cnt);
  35. }
  36.  
  37. void mandelbrot (int cols, int rows, float rMin, float rMax,
  38.                  float iMin, float iMax) {
  39.   float rInc = (rMax - rMin) / cols;
  40.   float iInc = (iMax - iMin) / rows;
  41.   int x, y, oldCount = -1, count = 0, runLength = 0;
  42.   complex zInit;
  43.  
  44.   PenMode(patCopy);
  45.   zInit.x = rMin;
  46.   for (x = 0; x < cols; x++) {
  47.     zInit.y = iMin;
  48.     MoveTo(x, 0);
  49.     for (y = 0; y < rows; y++) {
  50.           count = iterate (zInit);
  51.           if (count != oldCount) {
  52.               Line(0, runLength);
  53.               PmForeColor((count < 256) ? ((count + 60) % 256): 0);
  54.               runLength = 0;
  55.               oldCount = count;
  56.           }
  57.       runLength++;
  58.       zInit.y += iInc;
  59.     }
  60.     Line(0, runLength);
  61.     runLength = 0;
  62.     zInit.x += rInc;
  63.   }
  64. }
  65.  
  66. /* slow code
  67. complex complexSquare (complex c) {
  68.   complex cSq;
  69.  
  70.   cSq.x = c.x * c.x - c.y * c.y;
  71.   cSq.y = 2 * c.x * c.y;
  72.   return (cSq);
  73. }
  74.  
  75. int iterate (complex zInit) {
  76.   complex z = zInit;
  77.   int cnt = 0;
  78.  
  79.   //  quit when z * z > 4
  80.   while ((z.x * z.x + z.y * z.y <= 4.0) && (cnt < MAX)) {
  81.       if (zInit.x * zInit.x + zInit.y * zInit.y <= 4.0) {
  82.         z = complexSquare(z);
  83.         z.x += zInit.x;
  84.         z.y += zInit.y;
  85.         cnt++;
  86.     } else {
  87.         z = complexSquare(z);
  88.         z.x += 4;
  89.         z.y += 4;
  90.         cnt++;
  91.     }
  92.     
  93.   }
  94.   return (cnt);
  95. }
  96.  
  97.  
  98. void mandelbrot (int cols, int rows, double rMin, double rMax,
  99.                  double iMin, double iMax) {
  100.   double rInc = (rMax - rMin) / cols;
  101.   double iInc = (iMax - iMin) / rows;
  102.   int x, y, count;
  103.   complex zInit;
  104.   PenMode(patCopy);
  105.   zInit.x = rMin;
  106.   for (x = 0; x < cols; x++) {
  107.     zInit.y = iMin;
  108.     for (y = 0; y < rows; y++) {
  109.           count = iterate (zInit);
  110.           PmForeColor((count < 256) ? ((count + 60) % 256): 0);
  111.           MoveTo(x, y);
  112.           Line(1, 0);
  113.       zInit.y += iInc;
  114.     }
  115.     zInit.x += rInc;
  116.   }
  117. }*/